package com.spark.common.utils;

import com.alibaba.fastjson2.JSONArray;
import com.spark.common.constants.CacheConstants;
import com.spark.common.core.entity.system.SysDictItem;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 字典工具类
 *
 * @author LYCHEE
 * @date 2025/2/20 10:27
 */
public class DictUtils {

    /**
     * 设置字典缓存
     *
     * @param dictCode  缓存的Key
     * @param dictItems 缓存的数据
     */
    public static void setDictCache(String dictCode, List<SysDictItem> dictItems) {
        SpringUtils.getBean(RedisUtils.class).set(CacheConstants.spliceCacheKey(CacheConstants.SYS_DICT_KEY, dictCode), dictItems);
    }

    /**
     * 通过字典code获取字典项
     *
     * @param dictCode 字典Code
     * @return 字典项数据
     */
    public static List<SysDictItem> getDictItemByDictCode(String dictCode) {
        JSONArray dictItems = SpringUtils.getBean(RedisUtils.class).get(CacheConstants.spliceCacheKey(CacheConstants.SYS_DICT_KEY, dictCode));
        if (Objects.nonNull(dictItems)) {
            return dictItems.toJavaList(SysDictItem.class);
        }
        return null;
    }

    /**
     * 通过字典code获取字典项
     *
     * @param dictCodes 字典Code
     * @return 字典项数据
     */
    public static List<SysDictItem> getDictItemByDictCodes(List<String> dictCodes) {
        List<SysDictItem> itemList = new ArrayList<>();
        RedisUtils redisUtils = SpringUtils.getBean(RedisUtils.class);
        if (CollectionUtils.isEmpty(dictCodes)){
            return itemList;
        }
        for (String dictCode : dictCodes) {
            JSONArray dictItems = redisUtils.get(CacheConstants.spliceCacheKey(CacheConstants.SYS_DICT_KEY, dictCode));
            if (Objects.nonNull(dictItems)) {
                itemList.addAll(dictItems.toJavaList(SysDictItem.class));
            }
        }
        return itemList;
    }

    /**
     * 通过字典Code和字典项值获取字典项文本
     *
     * @param dictCode 字典Code
     * @param itemVal  字典项值
     * @return 字典项文本
     */
    public static String getDictItemText(String dictCode, String itemVal) {
        List<SysDictItem> dictCache = getDictItemByDictCode(dictCode);
        if (!CollectionUtils.isEmpty(dictCache)) {
            for (SysDictItem sysDictItem : dictCache) {
                if (itemVal.equals(sysDictItem.getItemVal())) {
                    return sysDictItem.getItemText();
                }
            }
        }
        return null;
    }

    /**
     * 通过字典Code和字典项文本获取字典项值
     *
     * @param dictCode 字典Code
     * @param itemText 字典项文本
     * @return 字典项值
     */
    public static String getDictItemVal(String dictCode, String itemText) {
        List<SysDictItem> dictCache = getDictItemByDictCode(dictCode);
        if (!CollectionUtils.isEmpty(dictCache)){
            for (SysDictItem sysDictItem : dictCache) {
                if (itemText.equals(sysDictItem.getItemText())){
                    return sysDictItem.getItemVal();
                }
            }
        }
        return null;
    }

    /**
     * 清空字典缓存
     */
    public static void clearDictCache(){
        Collection<String> keys = SpringUtils.getBean(RedisUtils.class).keys(CacheConstants.spliceCacheKey(CacheConstants.SYS_DICT_KEY,"*"));
        SpringUtils.getBean(RedisUtils.class).dels(keys);
    }

}
